package com.sqlist.share;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sqlist.share.dto.LoginResultDTO;
import com.sqlist.share.dto.MessageDTO;
import com.sqlist.share.dto.RegisterDTO;
import com.sqlist.share.util.Callback;
import com.sqlist.share.util.Const;
import com.sqlist.share.util.GsonUtils;
import com.sqlist.share.util.HttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by SqList on 2018/6/16.
 */

public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "Share - LoginActivity";

    private void setStatusBar() {
        // 禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 去掉Android原生的标题栏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        // 将状态栏设成透明
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = this.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }

        // android6.0以后可以对状态栏文字颜色和图标进行修改
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    private EditText usernameEditText;
    private EditText passwordEditText;
    private ImageView registerBtnImageView;
    private TextView toLoginBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBar();
        setContentView(R.layout.activity_register);
        Log.i(TAG, "onCreate()");

        init();
    }

    private void init() {
        usernameEditText = findViewById(R.id.usernameEditTextReg);
        passwordEditText = findViewById(R.id.passwordEditTextReg);
        registerBtnImageView = findViewById(R.id.registerBtnImageView);
        toLoginBtn = findViewById(R.id.toLoginBtn);

        registerBtnImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameEditText.getText().toString();
                String password = passwordEditText.getText().toString();

                final Map<String, String> registerMap = new HashMap<>();
                registerMap.put("username", username);
                registerMap.put("password", password);

                HttpUtils.getInstance().post("http://" + Const.HOST + ":" + Const.HTTP_PORT + "/register", registerMap, new Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        MessageDTO<RegisterDTO> registerDTOMessageDTO = (MessageDTO<RegisterDTO>) o;
                        Log.d(TAG, registerDTOMessageDTO.toString());

                        if (registerDTOMessageDTO.getCode() == Const.REQUEST_SUCCESS) {
                            RegisterDTO registerDTO = GsonUtils.getInstance().fromJson(String.valueOf(registerDTOMessageDTO.getData()), RegisterDTO.class);
                            if (registerDTO.getStatus() == RegisterDTO.REGISTER_SUCCESS) {
                                Toast.makeText(RegisterActivity.this, registerDTOMessageDTO.getMsg(), Toast.LENGTH_LONG).show();

                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                Toast.makeText(RegisterActivity.this, registerDTOMessageDTO.getMsg(), Toast.LENGTH_LONG).show();
                            }
                        } else {
                            Toast.makeText(RegisterActivity.this, registerDTOMessageDTO.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailed(Exception e) {
                        e.printStackTrace();
                    }
                }, MessageDTO.class);
            }
        });

        toLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
